package cn.tradewin.common.factories.impl;

import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.ioc.annotations.EagerLoad;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.Symbol;
import org.apache.tapestry5.ioc.services.PipelineBuilder;
import org.slf4j.Logger;
import org.tynamo.security.SecuritySymbols;
import org.tynamo.security.internal.services.LoginContextService;
import org.tynamo.security.services.impl.SecurityFilterChainFactoryImpl;
import org.tynamo.security.shiro.AccessControlFilter;
import org.tynamo.security.shiro.authc.BasicHttpAuthenticationFilter;
import org.tynamo.security.shiro.authc.FormAuthenticationFilter;
import org.tynamo.security.shiro.authc.UserFilter;
import org.tynamo.security.shiro.authz.PermissionsAuthorizationFilter;
import org.tynamo.security.shiro.authz.PortFilter;
import org.tynamo.security.shiro.authz.RolesAuthorizationFilter;
import org.tynamo.security.shiro.authz.SslFilter;

@EagerLoad
public class TwdSecurityFilterChainFactoryImpl  extends SecurityFilterChainFactoryImpl  {
	
	private final LoginContextService loginContextService;
	
	public TwdSecurityFilterChainFactoryImpl(Logger logger,
			PipelineBuilder builder, LoginContextService loginContextService,
			@Inject @Symbol(SymbolConstants.TAPESTRY_VERSION) String tapestryVersion,
		    @Inject @Symbol(SecuritySymbols.SUCCESS_URL) String successUrl,
		    @Inject @Symbol(SecuritySymbols.LOGIN_URL) String loginUrl,
		    @Inject @Symbol(SecuritySymbols.UNAUTHORIZED_URL) String unauthorizedUrl
			) {
		super(logger, builder, loginContextService, tapestryVersion, successUrl, loginUrl, unauthorizedUrl);
		this.loginContextService = loginContextService;
	}
	
	@Override
	public UserFilter user() {
		String name = "user";
		UserFilter filter = new UserFilter(loginContextService);
		filter.setUnauthorizedUrl(AccessControlFilter.UNAUTHORIZED_URL);
		filter.setName(name);
		return filter;
	}

	@Override
	public FormAuthenticationFilter authc() {
		String name = "authc";
		FormAuthenticationFilter filter = new FormAuthenticationFilter(loginContextService);
		filter.setUnauthorizedUrl(AccessControlFilter.UNAUTHORIZED_URL);
		filter.setName(name);
		return filter;
	}

	@Override
	public BasicHttpAuthenticationFilter basic() {
		String name = "authcBasic";
		BasicHttpAuthenticationFilter filter = new BasicHttpAuthenticationFilter(loginContextService);
		filter.setUnauthorizedUrl(AccessControlFilter.UNAUTHORIZED_URL);
		filter.setName(name);
		return filter;
	}

	@Override
	public RolesAuthorizationFilter roles() {
		String name = "roles";
		RolesAuthorizationFilter filter = new RolesAuthorizationFilter(loginContextService);
		filter.setUnauthorizedUrl(AccessControlFilter.UNAUTHORIZED_URL);
		filter.setName(name);
		return filter;
	}
	
	@Override
	public PermissionsAuthorizationFilter perms() {
		String name = "perms";
		PermissionsAuthorizationFilter filter = new PermissionsAuthorizationFilter(loginContextService);
		filter.setUnauthorizedUrl(AccessControlFilter.UNAUTHORIZED_URL);
		filter.setName(name);
		return filter;
	}

	@Override
	public SslFilter ssl() {
		SslFilter filter = new SslFilter(loginContextService);
		filter.setUnauthorizedUrl(AccessControlFilter.UNAUTHORIZED_URL);
		filter.setName("ssl");
		return filter;
	}

	@Override
	public PortFilter port() {
		PortFilter filter = new PortFilter(loginContextService);
		filter.setUnauthorizedUrl(AccessControlFilter.UNAUTHORIZED_URL);
		filter.setName("port");
		return filter;
	}
}
